package com.runa.protocol.analysis;

import com.runa.protocol.dto.DtuReport;
import com.runa.protocol.dto.IInterData;
import com.runa.protocol.util.ByteUtil;

/**
 * 济南有人dtu-G780
 */
public class DtuURBufferAnalysis extends ShellBufferAnalysis<byte[]> {


    public DtuURBufferAnalysis(MetaBufferAnalysis analysis) {
        super(analysis, 256);
    }

    @Override
    public int fill(byte[] data, int length,String n) {
        if (netEquNo == null) {
            String tag = new String(data, 0, length);
            if (tag.startsWith("UNIT-DTU")) {
                netEquNo = tag.substring(tag.lastIndexOf('-')+1);
                DtuReport report = new DtuReport(netEquNo);
                report.setMustAck(false);
                interData = report;
            } else {
                return -1;
            }
        } else {
            System.arraycopy(data, 0, buffer, 0, length);
            int ret = analysis.fill(buffer, length,netEquNo);
            interData = analysis.gain();
            return ret;
        }
        return 0;
    }

    public static void main(String[] args) {
        //计量请求—抄楼站阀  网络设备号：20220119, 阀编号：[2]
//        String s="02038041D72E84C2C800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042C80000469C36812271";
        String s="02036C000100010000000000000000000000004643500046435000000200010002000300010005000000010001000100020001000000000001000100000001000000030000000000000000000000000000000000000000000000004359B5564291238E0000000000000000420E00006D5D";
        DtuURBufferAnalysis dtuURBufferAnalysis = new DtuURBufferAnalysis(new ModbusBufferRTUAnalysis());
        byte[] bytes = ByteUtil.hexStringToByte(s);
        dtuURBufferAnalysis.fill(bytes,bytes.length,"");
        dtuURBufferAnalysis.gain();
    }

}
